package com.iclippi.test.server;

import java.util.Date;

import com.iclippi.server.model.Clipboard;
import com.iclippi.server.model.ClippyCache;

public class ClipboardModelTestCase extends LocalDatastoreTestCase {

	public void testCreate() {
		Clipboard clipboard = new Clipboard("test");
		assertEquals("Clipboard name", "test", clipboard.getName());
		assertEquals("Clipboard version", 1L, (long) clipboard.getVersion());

		clipboard = new Clipboard(
				"A really really long name for a clipboard is done now");
		assertEquals("Clipboard name",
				"A really really long name for a clipboard is done now"
						.substring(0, 32), clipboard.getName());
	}

	public void testLoad() {
		Date startDate = new Date();
		Clipboard clipboard = new Clipboard("test", "first content");
		Long clipboardID = clipboard.getId();
		assertEquals(startDate, clipboard.getUpdateDate());

		clipboard = Clipboard.getClipboard(clipboardID);
		assertEquals("test", clipboard.getName());
		assertEquals("first content", clipboard.getContent());

		clipboard = Clipboard.getClipboard("test");
		assertEquals(clipboardID, clipboard.getId());
		assertEquals("first content", clipboard.getContent());

		assertEquals("Clipboard version", (Long) 1L, (Long) Clipboard
				.getClipboardVersion("test"));

		ClippyCache.clearCache();
		assertEquals("Clipboard name", "test", Clipboard.getClipboard(
				clipboardID).getName());

		ClippyCache.clearCache();
		assertEquals("Clipboard version", (Long) 1L, (Long) Clipboard
				.getClipboardVersion("test"));

		clipboard = Clipboard.getClipboard("new test");
		assertEquals("Clipboard name", "new test", clipboard.getName());

		clipboard = Clipboard.getClipboard((String) null);
		assertEquals("Return null", null, clipboard);

	}

	public void testUpdate() {
		Clipboard clipboard = new Clipboard("test", "old content");
		clipboard.updateClipboard("new content");
		Long clipboardId = clipboard.getId();

		clipboard = Clipboard.getClipboard(clipboardId);
		assertEquals("Clipboard new content new", "new content", clipboard
				.getContent());
		assertEquals("Clipboard new version", (Long) 2L, (Long) clipboard
				.getVersion());

		ClippyCache.clearCache();
		clipboard.updateClipboard("newest content");
		assertEquals("Clipboard new version", (Long) 3L, (Long) clipboard
				.getVersion());

	}

	public void testSecurityContent() {
		String badHtml = "<html>"
				+ "<script>var a=1;</script>"
				+ "<frame src='http://google.com'></frame>"
				+ "<iframe src='http://google.com'></iframe>"
				+ "<EMBED SRC='xfiles.mid' VOLUME='50' HEIGHT='60' WIDTH='144'></embed>"
				+ "<object classid='clsid:F08DF954-8592-11D1-B16A-00C0F0283628'></object>"
				+ "<APPLET CODE='MyApplet.class' WIDTH=100 HEIGHT=100></applet>"
				+ "</html>";

		String expectedHtml = "<html>"
				+ "[script>var a=1;[/script>"
				+ "[frame src='http://google.com'>[/frame>"
				+ "[iframe src='http://google.com'>[/iframe>"
				+ "[embed SRC='xfiles.mid' VOLUME='50' HEIGHT='60' WIDTH='144'>[/embed>"
				+ "[object classid='clsid:F08DF954-8592-11D1-B16A-00C0F0283628'>[/object>"
				+ "[applet CODE='MyApplet.class' WIDTH=100 HEIGHT=100>[/applet>"
				+ "</html>";

		Clipboard clipboard = new Clipboard("test", badHtml);
		assertEquals("Unsafe html tags removal", expectedHtml, clipboard
				.getContent());

	}

}
